package com.yonyou.cyxdms.sysAuth.service.constants;

import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyx.function.utils.common.StringUtils;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class StringUtil {
    private static final char UNDERLINE = '_';

    private StringUtil() {
    }

    /**
     * 下划线 转 驼峰
     *
     * @param param
     * @return
     */
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        boolean flag = false;
        StringBuilder sb = new StringBuilder(len);

        for (int i = 0; i < len; i++) {
            if (flag) {
                sb.append(Character.toUpperCase(param.charAt(i)));
                flag = false;
            } else {
                char c = Character.toLowerCase(param.charAt(i));
                if (c == UNDERLINE) {
                    flag = true;
                } else {
                    sb.append(c);
                }
            }


        }
        return sb.toString();
    }


    /**
     * @param list
     * @param basicresult
     * @return void
     * @author Administrator
     * @since 2019/8/1
     */
    public static void doBasicresult(List<Map<String, Object>> list, Map<String, String> basicresult) {
        StringBuilder maintain = null;//维修
        StringBuilder accessories = null;//配件
        StringBuilder vehicleWarehouse = null;//整车仓库
        StringBuilder accessoriesWarehouse = null;//配件仓库
        StringBuilder suppliesWarehouse = null;//用品仓库
        StringBuilder favorableModels = null;//优惠模式

        for (int i = 0; i < list.size(); i++) {
            String ctrlCode = (String) list.get(i).get("CTRL_CODE");
            Integer type = (Integer) list.get(i).get("TYPE");
            maintain = simplify(maintain, ManageDictCodeConstants.MAINTAIN, type, ctrlCode);
            accessories = simplify(accessories, ManageDictCodeConstants.ACCESSORIES, type, ctrlCode);
            vehicleWarehouse = simplify(vehicleWarehouse, ManageDictCodeConstants.VEHICLE_WAREHOUSE, type, ctrlCode);
            accessoriesWarehouse = simplify(accessoriesWarehouse, ManageDictCodeConstants.ACCESSORIES_WAREHOUSE, type, ctrlCode);
            suppliesWarehouse = simplify(suppliesWarehouse, ManageDictCodeConstants.SUPPLIES_WAREHOUSE, type, ctrlCode);
            favorableModels = simplify(favorableModels, ManageDictCodeConstants.FAVORABLE_MODELS, type, ctrlCode);

        }
        basicresult.put("maintain", maintain == null ? null : maintain.toString());
        basicresult.put("accessories", accessories == null ? null : accessories.toString());
        basicresult.put("vehicleWarehouse", vehicleWarehouse == null ? null : vehicleWarehouse.toString());
        basicresult.put("accessoriesWarehouse", accessoriesWarehouse == null ? null : accessoriesWarehouse.toString());
        basicresult.put("suppliesWarehouse", suppliesWarehouse == null ? null : suppliesWarehouse.toString());
        basicresult.put("favorableModels", favorableModels == null ? null : favorableModels.toString());
    }

    /**
     * @param a
     * @param b
     * @param type
     * @param ctrlCode
     * @return java.lang.StringBuilder
     * @author dengqiang
     * @since 2019/8/2
     */
    public static StringBuilder simplify(StringBuilder a, Integer b, Integer type, String ctrlCode) {
        if (type.equals(b)) {
            if (a == null) {
                return new StringBuilder(ctrlCode);
            } else {
                return a.append(",").append(ctrlCode);
            }
        }
        return a;
    }


    /**
     * @param listmap
     * @param code
     * @return java.lang.String[]
     * @author dengqiang
     * @since 2019/8/2
     */
    public static String[] getTreeMenuSome(List<Map<String, Object>> listmap, String code) {
        String[] treeMenuSome = new String[listmap.size()];
        for (int i = 0; i < listmap.size(); i++) {
            String oneSome = null;
            Map map = listmap.get(i);
            String someCode = map.get(code).toString();//去重
            String[] aaArray = someCode.split(",");
            HashSet<String> hs = new HashSet<>();
            for (String s : aaArray) {
                hs.add(s);
            }
            Iterator<String> it = hs.iterator();
            if (it.hasNext()) {
                someCode = hs.toString().replace("[", "").replace("]", "");//去除相同项的字符串
            }

            oneSome = map.get("MENU_ID") + "-" + someCode;
            treeMenuSome[i] = oneSome;

        }

        return treeMenuSome;
    }


    public static void putMap(List<Map<String, Object>> listmap3, Map<String, Object> map, List<Map<String, Object>> listmap) {
        if (!CommonUtils.isNullOrEmpty(listmap3)) {
            map.put(CommonConstants.IS_FLEXIBLE, 10041002);
            map.put(CommonConstants.IS_EMPOWERED, 10041001);
        } else {
            loopPutMap(map, listmap);
        }
    }

    public static void loopPutMap(Map<String, Object> map, List<Map<String, Object>> listmap) {
        for (int j = 0; j < listmap.size(); j++) {
            if (StringUtils.isEquals(listmap.get(j).get(CommonConstants.IS_FLEXIBLE) == null ? CommonConstants.CONSTANT_2 : listmap.get(j).get(CommonConstants.IS_FLEXIBLE).toString(), "10041001")) {
                map.put(CommonConstants.IS_FLEXIBLE, 10041001);
            }
            if (StringUtils.isEquals(listmap.get(j).get(CommonConstants.IS_EMPOWERED) == null ? CommonConstants.CONSTANT_2 : listmap.get(j).get(CommonConstants.IS_EMPOWERED).toString(), "10041001")) {
                map.put(CommonConstants.IS_EMPOWERED, 10041001);
            }
        }
    }


    public static void putResultMap(Map<String, Object> resultMap, Map<String, Object> map, String param) {
        resultMap.put("isFlexible", map.get(CommonConstants.IS_FLEXIBLE));
        resultMap.put("isEmpowered", map.get(CommonConstants.IS_EMPOWERED));

        if (StringUtils.isEquals(map.get(CommonConstants.IS_FLEXIBLE).toString(), CommonConstants.CONSTANT_2)) {
            resultMap.put("isDisabled", 10041001);
        } else {
            resultMap.put("isDisabled", 10041002);
            resultMap.put(param, resultMap.get(param) + "[可分配]");
        }
    }
}